﻿@page
@{
    ViewData["Title"] = "字典管理";
    Layout = "~/Pages/Admin/_Layout.cshtml";
}

    <div id="app">


        <form class="layui-form form-cus" action="">

            <div class="layui-form-item">
                <label class="layui-form-label">父级</label>
                <div class="layui-input-block">
                    <div id="dicType" name="ParentId" class="xm-select-demo"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" v-model="m.name" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">唯一编码</label>
                <div class="layui-input-block">
                    <input type="text"  v-model="m.code" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">值</label>
                <div class="layui-input-block">
                    <input type="text" v-model="m.value"  lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" v-model="m.sort"  lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-input-block layui-cur-submit">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="submit" id="submit"><i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop layui-hide"></i>提交</button>
                    <button type="button" class="layui-btn layui-btn-primary btn-open-close">取消</button>
                </div>
            </div>
        
        </form>

    </div>

    <script>

        var vm = new Vue({
            el: '#app',
            data: {
                m: { }
            }
        });


        layui.use(['layer', 'jquery', 'common', 'form'], function () {
            var form = layui.form, $ = layui.jquery, os = layui.common;
            var index = parent.layer.getFrameIndex(window.name);
            //标识操作 add edit show 
            var opt = os.getUrlParam('opt');

            //下拉框
            var dicTypeSelect = os.selectSingleRender({
                el: '#dicType',
                tips: '请选择字典分组'

            });

            var oc = {
                //初始化
                init() {
                    //更新
                    if (opt=='edit') {
                        //查询用户信息
                        os.ajax('api/admin/sysdictionary/get',
                            { id: os.getUrlParam('id') },
                            function (res) {
                                if (res.code == 1) {
                                    vm.m = res.data;
                                    //加载所有角色下拉列表
                                    //console.log(res.data.parent);

                                    var node = {
                                        id: null,
                                        name: '父节点',
                                        selected:true
                                    };
                                    for (var index in res.data.parent) {
                                        if (res.data.parent[index].selected == true) {
                                            node.selected = false;
                                        }
                                    }
                                    res.data.parent.splice(0, 0, node);
                                    dicTypeSelect.update({
                                        data: res.data.parent
                                    })


                                } else {
                                    os.error(res.message);
                                }
                             

                            }, 'get');

                       
                    }
                    //添加
                    else {
                        os.ajax('api/admin/sysdictionary/QueryParents', {}, function (res) {
                          
                            var node = {
                                id:null,
                                name:'父节点'
                            };
                            res.data.splice(0, 0, node);
                            //加载所有角色下拉列表
                            dicTypeSelect.update({
                                data: res.data
                            })
                        }, 'get');

                    }
                  

                },




            }
            //初始化
            oc.init();

            //监听提交
            form.on('submit(submit)', function (data) {
                $('#submit').attr('disabled', true).find('i').removeClass('layui-hide');
                var urls = "api/admin/sysdictionary/add";
                if (opt=='edit') {
                    urls = "api/admin/sysdictionary/update";
                }
                //获取选中的值
                if (dicTypeSelect.getValue().length > 0) {
                    vm.m.parentId = dicTypeSelect.getValue()[0].id;
                }
             
                os.ajax(urls, vm.m, function (res) {
                    $('#submit').attr('disabled', false).find('i').addClass('layui-hide');
                    if (res.code ==1) {
                        parent.layer.close(index);
                        //刷新父页面的数据
                        $('button[data-type="reload"]', window.parent.document).click();
                    }
                    else {
                        os.error(res.msg);
                    }
                },'post');
                return false;
            });
            $(".btn-open-close").on('click', function () {
                parent.layer.close(index);
            });
        });


    </script>
